﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using BusinessManage.Common;
using BusinessManage.ConfirmSign;
using CPFrameWork.Global;
using CPFrameWork.UIInterface.Form;
using CPFrameWork.Utility.DbOper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UEditorNetCore;

namespace BusinessManage.UpdateForm
{
    //更新表
    public class UpdateForm : CPFrameWork.UIInterface.Form.ICPFormAfterSave
    {
        public void AfterSave(ICPFormAfterSaveEventArgs e)
        {

        }
    }



    //发车 更新任务配送状态和车辆配送状态
    public class UpdateSetoutForm : CPFrameWork.UIInterface.Form.ICPFormAfterSave
    {
        public void AfterSave(ICPFormAfterSaveEventArgs e)
        {
            string TaskId = e.GetFieldValue(e.Form.MainTableName, "TaskId", 0);
            string OrderId = e.GetFieldValue(e.Form.MainTableName, "OrderId", 0);
            string userId = e.GetFieldValue(e.Form.MainTableName, "OperateUserId", 0);
            string currentTime = DateTime.Now.ToString();
            //更新任务信息表(未发车时，更新，即第一辆车发车时进行更新)
            string strSql = "Update YW_TaskInfo";
            strSql += " set DeliveryTaskStatus = '1'";
            strSql += ",LeaveTime = '" + currentTime + "'";
            strSql += " where TaskId  = '" + TaskId + "' and DeliveryTaskStatus= '0';";
            //更新任务与订单关系表
            //strSql += " Update YW_TaskOrderRelation";            
            //strSql += " set OrderDeliveryStatus = '1'";
            //strSql += " where TaskId  = '" + TaskId + "';";
            //更新任务与车辆关系表
            strSql += " Update YW_TaskCarRelation";
            strSql += " set CarDeliveryStatus = '1'";
            strSql += ",CarLeaveTime = '" + currentTime + "'";
            strSql += " where TaskId  = '" + TaskId + "' and (MainDriverId= '" + userId + "' or AssistantDriverId= '" + userId + "')";            
            StringBuilder sb = new StringBuilder();
            sb.Append(strSql);
            DbHelper dbHelper = new DbHelper(e.Form.DbIns, CPAppContext.CurDbType());
            if (sb.Length > 0)
            {
                dbHelper.ExecuteNonQuery(sb.ToString());
            }
            //如果是父母代订单，更新实际发货时间
            string orderType = "";
            string strSqlorder = "select OrderCode,type from YW_OrderInfo where OrderCode='" + OrderId + "'";
            StringBuilder sb_order = new StringBuilder();
            sb_order.Append(strSqlorder);
            DataTable dtOrder = dbHelper.ExecuteDataSet(sb_order.ToString()).Tables[0];
            if (dtOrder.Rows.Count > 0)
            {
                orderType= dtOrder.Rows[0][1].ToString();
                if (orderType=="父母代") {
                    string strSqltime = " Update YW_OrderInfo";
                    strSqltime += " set DeliveryTime = '"+ currentTime + "'";
                    strSqltime += " where OrderCode='" + OrderId + "' and DeliveryTime is null;";
                    StringBuilder sb_time = new StringBuilder();
                    sb_time.Append(strSqltime);
                    if (sb_time.Length > 0)
                    {
                        dbHelper.ExecuteNonQuery(sb_time.ToString());
                    }
                }
            }
        }
    }



    /// <summary>
    /// 订单签收
    /// 参数：TaskId，OrderId，CarNumber，MainDriverId，AssistantDriverId，CustomerName，CustomerState，CustomerCity，CustomerDistrict，CustomerAddress，CustomerZip，CustomerMobile，CustomerPhone，SignCode(base64编码)，SignType  
    /// </summary>
    public class CustomerSignForm : CPFrameWork.UIInterface.Form.ICPFormAfterSave
    {
        public void AfterSave(ICPFormAfterSaveEventArgs e)
        {
            StringBuilder stringb = new StringBuilder();
            StringBuilder stringb_check = new StringBuilder();
            StringBuilder stringb_bringin = new StringBuilder();
            StringBuilder stringb_order = new StringBuilder();
            DbHelper dbHelper = new DbHelper(e.Form.DbIns, CPAppContext.CurDbType());

            //获取参数
            string TaskId = e.GetFieldValue(e.Form.MainTableName, "TaskId", 0);
            string OrderId = e.GetFieldValue(e.Form.MainTableName, "OrderId", 0);
            string SignCode = e.GetFieldValue(e.Form.MainTableName, "SignCode", 0);
            string UserId = e.GetFieldValue(e.Form.MainTableName, "OperateUserId", 0);
            string UserName = e.GetFieldValue(e.Form.MainTableName, "OperateUserName", 0);
            string SignId = e.GetFieldValue(e.Form.MainTableName, "SignId", 0);  //客户签收表主键，拆户、换户时需要传
            string signFilePath = "SignImages";

            //保存签字图片            
            string FilePath = CPAppContext.HostingEnvironment.ContentRootPath + "\\wwwroot\\Style\\" + signFilePath + "\\";
            if (System.IO.Directory.Exists(FilePath) == false)
                System.IO.Directory.CreateDirectory(FilePath);
            string SignPath = "/Style/" + signFilePath + "/" + Base64Code.SaveFile(SignCode, FilePath, ".png");
            string currentTime = DateTime.Now.ToString();
            string SignType = "";
            string strSql = "";


            //获取车辆配送状态
            string strSql_check = "SELECT TOP 1 t.Id,TaskId,OrderId,TaskOrderId,CarDeliveryStatus,CarNumber,CustomerName,CustomerState,CustomerCity,CustomerDistrict,CustomerAddress,CustomerZip,CustomerMobile,CustomerPhone,o.type from YW_TaskCarRelation as t left join YW_OrderInfo as o on o.OrderCode = t.OrderId";
            strSql_check += " where TaskId  = '" + TaskId + "' and OrderId  = '" + OrderId + "' and (MainDriverId= '" + UserId + "' or AssistantDriverId= '" + UserId + "');";
            stringb_check.Append(strSql_check);
            DataTable dtCar = dbHelper.ExecuteDataSet(stringb_check.ToString()).Tables[0];
            string CarDeliveryStatus = "";
            string CarNumber = "";
            string CustomerName = "";
            string CustomerState = "";
            string CustomerCity = "";
            string CustomerDistrict = "";
            string CustomerAddress = "";
            string CustomerZip = "";
            string CustomerMobile = "";
            string CustomerPhone = "";
            string orderType = "";

            if (dtCar.Rows.Count > 0)
            {
                CarDeliveryStatus = dtCar.Rows[0][4].ToString();
                CarNumber = dtCar.Rows[0][5].ToString();
                CustomerName = dtCar.Rows[0][6].ToString();
                CustomerState = dtCar.Rows[0][7].ToString();
                CustomerCity = dtCar.Rows[0][8].ToString();
                CustomerDistrict = dtCar.Rows[0][9].ToString();
                CustomerAddress = dtCar.Rows[0][10].ToString();
                CustomerZip = dtCar.Rows[0][11].ToString();
                CustomerMobile = dtCar.Rows[0][12].ToString();
                CustomerPhone = dtCar.Rows[0][13].ToString();
                orderType = dtCar.Rows[0][14].ToString();
            }
            else {
                string strSql_bringin = "SELECT TOP 1 t.Id,TaskId,OrderId,TaskOrderId,CarDeliveryStatus,TargetCarNumber,CustomerName,CustomerState,CustomerCity,CustomerDistrict,CustomerAddress,CustomerZip,CustomerMobile,CustomerPhone,TargetTaskId,o.type from YW_BringOutIn as t left join YW_OrderInfo as o on o.OrderCode = t.OrderId where TargetTaskId  = '" + TaskId + "' and OrderId  = '" + OrderId + "';";
                stringb_bringin.Append(strSql_bringin);
                DataTable dtCarIn = dbHelper.ExecuteDataSet(stringb_bringin.ToString()).Tables[0];
                if (dtCarIn.Rows.Count > 0)
                {
                    CarDeliveryStatus = dtCarIn.Rows[0][4].ToString();
                    CarNumber = dtCarIn.Rows[0][5].ToString();
                    CustomerName = dtCarIn.Rows[0][6].ToString();
                    CustomerState = dtCarIn.Rows[0][7].ToString();
                    CustomerCity = dtCarIn.Rows[0][8].ToString();
                    CustomerDistrict = dtCarIn.Rows[0][9].ToString();
                    CustomerAddress = dtCarIn.Rows[0][10].ToString();
                    CustomerZip = dtCarIn.Rows[0][11].ToString();
                    CustomerMobile = dtCarIn.Rows[0][12].ToString();
                    CustomerPhone = dtCarIn.Rows[0][13].ToString();
                    TaskId= dtCarIn.Rows[0][14].ToString();
                    orderType = dtCar.Rows[0][15].ToString();
                }
            }
            //车辆配送状态为1时签收，为正常签收
            if (CarDeliveryStatus == "1")
            {
                SignType = "1";
                //"客户签收表"插入一条记录
                strSql += "INSERT INTO YW_CustomerSign ";
                strSql += "(TaskId,OrderId,CarNumber,SignPath,CustomerName,CustomerState,CustomerCity,CustomerDistrict,CustomerAddress,CustomerZip,CustomerMobile,CustomerPhone,SignType,IsSign,CreateUserId,CreateUserName,SignTime) ";
                strSql += "values('" + TaskId + "'";
                strSql += ",'" + OrderId + "'";
                strSql += ",'" + CarNumber + "'";
                strSql += ",'" + SignPath + "'";
                strSql += ",'" + CustomerName + "'";
                strSql += ",'" + CustomerState + "'";
                strSql += ",'" + CustomerCity + "'";
                strSql += ",'" + CustomerDistrict + "'";
                strSql += ",'" + CustomerAddress + "'";
                strSql += ",'" + CustomerZip + "'";
                strSql += ",'" + CustomerMobile + "'";
                strSql += ",'" + CustomerPhone + "'";
                strSql += ",'" + SignType + "'";
                strSql += ",'1'";
                strSql += ",'" + UserId + "'";
                strSql += ",'" + UserName+ "'";
                strSql += ",'" + currentTime + "'";
                strSql += ");";
                //更新"任务与车辆关系表",车辆配送状态为"已完成",完成类型为"正常签收"
                strSql += " Update YW_TaskCarRelation";
                strSql += " set CarDeliveryStatus = '2' ";
                strSql += ",FinishType = '1'";
                strSql += " where TaskId  = '" + TaskId + "' and OrderId  = '" + OrderId + "' and (MainDriverId= '" + UserId + "' or AssistantDriverId= '" + UserId + "');";
            }

            //车辆配送状态为4时签收，为拆户签收
            if (CarDeliveryStatus == "4")
            {
                //SignType = "2";
                //"客户签收表"更新记录
                strSql += " Update YW_CustomerSign";
                strSql += " set IsSign = '1' ";
                strSql += ",SignPath = '" + SignPath + "'";
                //strSql += ",SignType = '2'";
                strSql += ",SignTime = '" + currentTime + "'";
                strSql += " where Id  = '" + SignId + "';";
                //更新"任务与车辆关系表",车辆配送状态为"已完成",完成类型为"拆户签收"
                //所有此车运输的此订单的拆户都签收后(不存在未签收的)，再更新车辆配送状态
                strSql += " Update YW_TaskCarRelation";
                strSql += " set CarDeliveryStatus = '2' ";
                strSql += ",FinishType = '2' ";
                strSql += " where not exists(select * from YW_CustomerSign where TaskId = '" + TaskId + "' and OrderId='" + OrderId + "' and CarNumber='"+ CarNumber + "' and IsSign='0') and TaskId  = '" + TaskId + "' and OrderId  = '" + OrderId + "' and (MainDriverId= '" + UserId + "' or AssistantDriverId= '" + UserId + "');";
            }

            //车辆配送状态为5时签收，为换户签收
            if (CarDeliveryStatus == "5")
            {
                SignType = "3";
                //"客户签收表"更新记录
                strSql += " Update YW_CustomerSign";
                strSql += " set IsSign = '1' ";
                strSql += ",SignPath = '"+SignPath+"'";
                strSql += ",SignType = '3'";
                strSql += ",SignTime = '"+ currentTime + "'";
                strSql += " where Id  = '" + SignId + "';";
                //更新"任务与车辆关系表",车辆配送状态为"已完成",完成类型为"换户签收"
                strSql += " Update YW_TaskCarRelation";
                strSql += " set CarDeliveryStatus = '2' ";
                strSql += ",FinishType = '3' ";
                strSql += " where TaskId  = '" + TaskId + "' and OrderId  = '" + OrderId + "' and (MainDriverId= '" + UserId + "' or AssistantDriverId= '" + UserId + "');";
            }

            //车辆配送状态为6时签收，为倒入签收
            if (CarDeliveryStatus == "6")
            {
                SignType = "4";
                //"客户签收表"插入一条记录
                strSql += "INSERT INTO YW_CustomerSign ";
                strSql += "(TaskId,OrderId,CarNumber,SignPath,CustomerName,CustomerState,CustomerCity,CustomerDistrict,CustomerAddress,CustomerZip,CustomerMobile,CustomerPhone,SignType,IsSign,CreateUserId,CreateUserName,SignTime) ";
                strSql += "values('" + TaskId + "'";
                strSql += ",'" + OrderId + "'";
                strSql += ",'" + CarNumber + "'";
                strSql += ",'" + SignPath + "'";
                strSql += ",'" + CustomerName + "'";
                strSql += ",'" + CustomerState + "'";
                strSql += ",'" + CustomerCity + "'";
                strSql += ",'" + CustomerDistrict + "'";
                strSql += ",'" + CustomerAddress + "'";
                strSql += ",'" + CustomerZip + "'";
                strSql += ",'" + CustomerMobile + "'";
                strSql += ",'" + CustomerPhone + "'";
                strSql += ",'" + SignType + "'";
                strSql += ",'1'";
                strSql += ",'" + UserId + "'";
                strSql += ",'" + UserName + "'";
                strSql += ",'" + currentTime + "'";
                strSql += ");";
                //更新"任务与车辆关系表",车辆配送状态为"已完成",完成类型为"倒入签收"
                strSql += " Update YW_TaskCarRelation";
                strSql += " set CarDeliveryStatus = '2' ";
                strSql += ",FinishType = '4' ";
                strSql += " where TaskId  = '" + TaskId + "' and OrderId  = '" + OrderId + "' and (MainDriverId= '" + UserId + "' or AssistantDriverId= '" + UserId + "');";
                //更新"倒入倒出信息表",车辆配送状态为"已完成",完成类型为"倒入签收"
                strSql += " Update YW_BringOutIn";
                strSql += " set CarDeliveryStatus = '2' ";
                strSql += ",FinishType = '4' ";
                strSql += " where TargetTaskId  = '" + TaskId + "' and OrderId  = '" + OrderId + "';";
            }

            //任务变更的签收
            //if (CarDeliveryStatus == "")
            //{
            //    string oldOrderId = "";
            //    //获取订单中客户信息
            //    string strSql_order = "SELECT TOP 1 t.Id,t.TaskId,OrderId,TaskOrderId,CarDeliveryStatus,t.CarNumber,CustomerName,CustomerState,CustomerCity,CustomerDistrict,CustomerAddress,CustomerZip,CustomerMobile,CustomerPhone,o.type,r.OldOrderId  from YW_TaskCarRelation as t left join YW_OrderInfo as o on o.OrderCode = t.OrderId";
            //    strSql_order += " where TaskId  = '" + TaskId + "' and r.NewOrderId    = '" + OrderId + "' and (MainDriverId= '" + UserId + "' or AssistantDriverId= '" + UserId + "');";
            //    stringb_order.Append(strSql_order);
            //    DataTable dtOrder= dbHelper.ExecuteDataSet(stringb_order.ToString()).Tables[0];
            //    if (dtOrder.Rows.Count > 0)
            //    {
            //        CarNumber = dtOrder.Rows[0][5].ToString();
            //        CustomerName = dtOrder.Rows[0][6].ToString();
            //        CustomerState = dtOrder.Rows[0][7].ToString();
            //        CustomerCity = dtOrder.Rows[0][8].ToString();
            //        CustomerDistrict = dtOrder.Rows[0][9].ToString();
            //        CustomerAddress = dtOrder.Rows[0][10].ToString();
            //        CustomerZip = dtOrder.Rows[0][11].ToString();
            //        CustomerMobile = dtOrder.Rows[0][12].ToString();
            //        CustomerPhone = dtOrder.Rows[0][13].ToString();
            //        orderType = dtOrder.Rows[0][14].ToString();
            //        oldOrderId = dtOrder.Rows[0][15].ToString();
            //    }
            //    SignType = "7";
            //    //"客户签收表"插入一条记录
            //    strSql += "INSERT INTO YW_CustomerSign ";
            //    strSql += "(TaskId,OrderId,CarNumber,SignPath,CustomerName,CustomerState,CustomerCity,CustomerDistrict,CustomerAddress,CustomerZip,CustomerMobile,CustomerPhone,SignType,IsSign,CreateUserId,CreateUserName,SignTime) ";
            //    strSql += "values('" + TaskId + "'";
            //    strSql += ",'" + OrderId + "'";
            //    strSql += ",'" + CarNumber + "'";
            //    strSql += ",'" + SignPath + "'";
            //    strSql += ",'" + CustomerName + "'";
            //    strSql += ",'" + CustomerState + "'";
            //    strSql += ",'" + CustomerCity + "'";
            //    strSql += ",'" + CustomerDistrict + "'";
            //    strSql += ",'" + CustomerAddress + "'";
            //    strSql += ",'" + CustomerZip + "'";
            //    strSql += ",'" + CustomerMobile + "'";
            //    strSql += ",'" + CustomerPhone + "'";
            //    strSql += ",'" + SignType + "'";
            //    strSql += ",'1'";
            //    strSql += ",'" + UserId + "'";
            //    strSql += ",'" + UserName + "'";
            //    strSql += ",'" + currentTime + "'";
            //    strSql += ");";
            //    //更新"任务与车辆关系表",车辆配送状态为"已完成",完成类型为"正常签收"
            //    strSql += " Update YW_TaskCarRelation";
            //    strSql += " set CarDeliveryStatus = '2' ";
            //    strSql += ",FinishType = '1'";
            //    strSql += " where TaskId  = '" + TaskId + "' and OrderId  = '" + oldOrderId + "' and (MainDriverId= '" + UserId + "' or AssistantDriverId= '" + UserId + "');";
            //}

            stringb.Append(strSql);
            if (stringb.Length > 0)
            {
                dbHelper.ExecuteNonQuery(stringb.ToString());
            }                  

            //签收以后调取确认收货接口。拆户的情况，全部签收以后再调取确认收货接口(查看签收信息表中，此任务、此订单未签收的数量为0)
            if (orderType!="父母代") {
                //获取买家id：user_id、店铺id：shop_id
                string shop_id = "";
                string user_id = "";
                string buyuserInfo = "SELECT distinct user_id,shop_id from O_systrade_trade where tid = '" + OrderId + "';";
                StringBuilder stringb_user = new StringBuilder();
                stringb_user.Append(buyuserInfo);
                DataTable dtUserInfo = dbHelper.ExecuteDataSet(stringb_user.ToString()).Tables[0];
                if (dtUserInfo.Rows.Count > 0)
                {
                    user_id = dtUserInfo.Rows[0][0].ToString();
                    shop_id = dtUserInfo.Rows[0][1].ToString();
                }
                string notSignCount = "";
                string notSignInfo = "SELECT COUNT(*) as notSignCount from YW_CustomerSign where TaskId = '" + TaskId + "' and OrderId = '" + OrderId + "' and IsSign = '0';";
                StringBuilder stringb_notsign = new StringBuilder();
                stringb_notsign.Append(notSignInfo);
                DataTable dtNotSignInfo = dbHelper.ExecuteDataSet(stringb_notsign.ToString()).Tables[0];
                if (dtNotSignInfo.Rows.Count > 0)
                {
                    notSignCount = dtNotSignInfo.Rows[0][0].ToString();
                    if (notSignCount == "0")
                    {
                        //调取确认收货接口
                        ConfirmRecieve confirmRecieve = new ConfirmRecieve();
                        confirmRecieve.SynRecieve(TaskId, OrderId, shop_id, user_id, UserId, UserName);
                    }
                }
            }
           
        }
    }

    /// <summary>
    /// 回场签到
    /// </summary>
    public class UpdateBackForm : CPFrameWork.UIInterface.Form.ICPFormAfterSave
    {
        public void AfterSave(ICPFormAfterSaveEventArgs e)
        {
            string TaskId = e.GetFieldValue(e.Form.MainTableName, "TaskId", 0);
            string OrderId = e.GetFieldValue(e.Form.MainTableName, "OrderId", 0);
            string userId = e.GetFieldValue(e.Form.MainTableName, "OperateUserId", 0);
            string currentTime = DateTime.Now.ToString();
            //更新任务与车辆关系表
            string strSql = "Update YW_TaskCarRelation";
            strSql += " set CarDeliveryStatus = '3'";
            strSql += ",CarArriveTime = '" + currentTime + "'";
            strSql += " where TaskId  = '" + TaskId + "' and (MainDriverId= '" + userId + "' or AssistantDriverId= '" + userId + "');";
            //若存在倒入倒出的情况，则更新倒入倒出车辆关系表
            strSql += "if exists (select * from YW_BringOutIn where TargetTaskId='" + TaskId + "') ";
            strSql += "Update YW_BringOutIn";
            strSql += " set CarDeliveryStatus = '3'";
            strSql += ",CarArriveTime = '" + currentTime + "'";
            strSql += " where TargetTaskId  = '" + TaskId +"';";

            //如果不存在其他配送状态的车辆，则更新任务配送状态、到场时间
            strSql += " Update YW_TaskInfo";
            strSql += " set DeliveryTaskStatus = '2'";
            strSql += ",ArriveTime = '" + currentTime + "'";
            strSql += " where not exists(select * from YW_TaskCarRelation where (CarDeliveryStatus != '3') and TaskId = '" + TaskId + "') and  TaskId = '" + TaskId + "';";
            StringBuilder sb = new StringBuilder();
            sb.Append(strSql);
            DbHelper dbHelper = new DbHelper(e.Form.DbIns, CPAppContext.CurDbType());
            if (sb.Length > 0)
            {
                dbHelper.ExecuteNonQuery(sb.ToString());
            }
        }
    }


    /// <summary>
    /// 倒出
    /// 1、将此车、此订单下的各品种数据存入“倒入/倒出信息表”，设置目标车辆
    /// 2、更新任务与车辆关系表“完成类型”字段
    /// </summary>
    public class CarCarryOutForm : CPFrameWork.UIInterface.Form.ICPFormAfterSave
    {
        public void AfterSave(ICPFormAfterSaveEventArgs e)
        {
            //获取上传信息
            string TaskId = e.GetFieldValue(e.Form.MainTableName, "TaskId", 0);
            string OrderId = e.GetFieldValue(e.Form.MainTableName, "OrderId", 0);
            string UserId = e.GetFieldValue(e.Form.MainTableName, "OperateUserId", 0);
            string TargetCarId = e.GetFieldValue(e.Form.MainTableName, "TargetCarId", 0);
            string TargetCarNumber = e.GetFieldValue(e.Form.MainTableName, "TargetCarNumber", 0);
            string currentTime = DateTime.Now.ToString();
            //将此车、此订单下的YW_TaskCarRelation数据复制至YW_BringOutIn表,倒入状态为0未倒入
            string strSql = "insert into YW_BringOutIn(TaskId,OrderId,TaskOrderId,BaseId,BaseName,HallId,HallName,CarNumber,SensorId,GPSId,MainDriverId,MainDriverName,MainDriverTel,AssistantDriverId,AssistantDriverName,AssistantDriverTel,OrderChickCountInCar,HallLossCount,CarLossCount,CarDeliveryStatus,FinishType,SignPath,SignTime,CarLeaveTime,TargetCarNumber,Status,CreateUserId,CreateTime) ";
            strSql += " select TaskId, OrderId, TaskOrderId, BaseId, BaseName, HallId, HallName, CarNumber, SensorId, GPSId, MainDriverId, MainDriverName,MainDriverTel, AssistantDriverId, AssistantDriverName, AssistantDriverTel, OrderChickCountInCar, HallLossCount, CarLossCount, CarDeliveryStatus,FinishType, SignPath, SignTime,CarLeaveTime,'" + TargetCarNumber + "','0','" + UserId + "','" + currentTime + "' from YW_TaskCarRelation ";
            strSql += " where TaskId  = '" + TaskId + "' and OrderId  = '" + OrderId + "' and (MainDriverId= '" + UserId + "' or AssistantDriverId= '" + UserId + "');";
            //更新YW_TaskCarRelation表的车辆配送状态为2已完成，完成类型为6倒出完成
            strSql += " Update YW_TaskCarRelation";
            strSql += " set CarDeliveryStatus = '2' ";
            strSql += ",FinishType = '6' ";
            strSql += " where TaskId  = '" + TaskId + "' and OrderId  = '" + OrderId + "' and (MainDriverId= '" + UserId + "' or AssistantDriverId= '" + UserId + "');";
            StringBuilder sb = new StringBuilder();
            sb.Append(strSql);
            DbHelper dbHelper = new DbHelper(e.Form.DbIns, CPAppContext.CurDbType());
            if (sb.Length > 0)
            {
                dbHelper.ExecuteNonQuery(sb.ToString());
            }
        }
    }

    /// <summary>
    /// 倒入申请
    /// 1、更新倒入、倒出车辆信息表
    /// 2、更新任务与车辆关系表 车辆运输状态
    /// </summary>
    public class CarCarryInForm : CPFrameWork.UIInterface.Form.ICPFormAfterSave
    {
        public void AfterSave(ICPFormAfterSaveEventArgs e)
        {
            StringBuilder stringb_task = new StringBuilder();
            DbHelper dbHelper = new DbHelper(e.Form.DbIns, CPAppContext.CurDbType());
            //获取上传信息
            string TaskId = e.GetFieldValue(e.Form.MainTableName, "TaskId", 0);
            string OrderId = e.GetFieldValue(e.Form.MainTableName, "OrderId", 0);
            string UserId = e.GetFieldValue(e.Form.MainTableName, "OperateUserId", 0);
            string CarryOutUserId = e.GetFieldValue(e.Form.MainTableName, "CarryOutUserId", 0); //做倒出操作的司机编号
            //获取倒入者当前任务信息---------------------------------
            string strSql_task = "select distinct t.TaskId,t.Id,t.PlanDeliveryDate,t.DeliveryTaskStatus,t.OrderChickTotalCount,t.OrderLossChickTotalCount,t.LeaveTime,t.ArriveTime from YW_TaskInfo as t inner join YW_TaskCarRelation as c on c.TaskId = t.TaskId where(t.DeliveryTaskStatus = '0' or t.DeliveryTaskStatus = '1') and c.CarDeliveryStatus != '3' and(c.MainDriverId = '"+ UserId + "' or c.AssistantDriverId = '" + UserId + "') and ChangeStatus!= '1'";
            stringb_task.Append(strSql_task);
            DataTable dtTaskInfo = dbHelper.ExecuteDataSet(stringb_task.ToString()).Tables[0];
            string TaskId_In = "";
            if (dtTaskInfo.Rows.Count > 0) {
                TaskId_In = dtTaskInfo.Rows[0][0].ToString();
            }                

            //更新YW_BringOutIn表,将倒入状态更新为1已倒入，车辆配送状态为6已倒入
            string strSql = " Update YW_BringOutIn";
            strSql += " set Status = '1' ";
            strSql += ",TargetDriverId = '" + UserId + "' ";
            strSql += ",CarDeliveryStatus = '6' ";
            strSql += ",TargetTaskId = '"+ TaskId_In + "' ";
            strSql += " where TaskId  = '" + TaskId + "' and OrderId  = '" + OrderId + "' and (CreateUserId= '" + CarryOutUserId + "');";

            //更新YW_TaskCarRelation表的车辆配送状态为6已倒入(更新倒入者的车辆对应的配送状态)
            strSql += " Update YW_TaskCarRelation";
            strSql += " set CarDeliveryStatus = '6' ";
            strSql += " where TaskId  = '" + TaskId + "' and OrderId  = '" + OrderId + "' and (MainDriverId= '" + UserId + "' or AssistantDriverId= '" + UserId + "');";
            StringBuilder sb = new StringBuilder();
            sb.Append(strSql);
            if (sb.Length > 0)
            {
                dbHelper.ExecuteNonQuery(sb.ToString());
            }
        }
    }

    /// <summary>
    /// 任务变更
    /// 更新任务变更状态
    /// </summary>
    public class ChangeTaskForm : CPFrameWork.UIInterface.Form.ICPFormAfterSave
    {
        public void AfterSave(ICPFormAfterSaveEventArgs e)
        {
            string TaskId = e.GetFieldValue(e.Form.MainTableName, "TaskId", 0);
            string ChangeReasonId = e.GetFieldValue(e.Form.MainTableName, "ChangeReasonId", 0);
            string ChangeReason = e.GetFieldValue(e.Form.MainTableName, "ChangeReason", 0);
            string Describe = e.GetFieldValue(e.Form.MainTableName, "Describe", 0);
            string UserId = e.GetFieldValue(e.Form.MainTableName, "OperateUserId", 0);
            string UserName = e.GetFieldValue(e.Form.MainTableName, "OperateUserName", 0);
            string currentTime = DateTime.Now.ToString();

            //更新任务基本信息表 任务变更状态ChangeStatus为“1申请未处理”
            string strSql = "Update YW_TaskInfo";
            strSql += " set ChangeStatus = '1'";
            strSql += ", ChangeReasonId = '" + ChangeReasonId + "'";
            strSql += ", ChangeReason = '" + ChangeReason + "'";
            strSql += ", Describe = '" + Describe + "'";
            strSql += ", ChangeUserId = '" + UserId + "'";
            strSql += ", ChangeUserName = '" + UserName + "'";
            strSql += ", ChangeTime = '" + currentTime + "'";
            strSql += " where TaskId  = '" + TaskId + "';";
            StringBuilder sb = new StringBuilder();
            sb.Append(strSql);
            DbHelper dbHelper = new DbHelper(e.Form.DbIns, CPAppContext.CurDbType());
            if (sb.Length > 0)
            {
                dbHelper.ExecuteNonQuery(sb.ToString());
            }
        }
    }


}
